package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.OrderDao;
import com.wn.dao.RequirementDao;
import com.wn.entity.Order;
import com.wn.entity.Requirement;
import com.wn.service.RequirementService;
import com.wn.utils.PageParam;
import com.wn.utils.Rsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (Requirement)表服务实现类
 *
 * @author makejava
 * @since 2020-10-31 15:01:43
 */
@Service("requirementService")
@Transactional(rollbackFor = Exception.class)
public class RequirementServiceImpl implements RequirementService {
    @Resource
    private RequirementDao requirementDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Requirement queryById(String id) {
        return this.requirementDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public PageInfo<Requirement> queryAllByLimit(PageParam<Requirement> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Requirement> requirementList=requirementDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Requirement> RequirementPageInfo = new PageInfo<Requirement>(requirementList);

            return RequirementPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Requirement> requirementList=requirementDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Requirement> RequirementPageInfo = new PageInfo<Requirement>(requirementList);

            return RequirementPageInfo;

        }


    }


    /**
     * 新增数据
     *
     * @param requirement 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(Requirement requirement) {
        String rId = UUID.randomUUID().toString().replaceAll("-", "")+"_req";
        String oId = UUID.randomUUID().toString().replaceAll("-","")+"_order";
        requirement.setId(rId);
        requirement.setCreatetime(new Date());

        Order order = new Order();
        order.setId(oId);
        order.setOrderno(oId.substring(0,10));
        order.setCreateuserid(requirement.getCreateuserid());
        order.setStatus(rId);
        order.setPayType(0);
        order.setOrderstatus(0);
        order.setIsprivate(0);

        orderDao.insert(order);
        requirement.setOrderid(oId);

        return requirementDao.insert(requirement)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param requirement 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(Requirement requirement) {

        Order order = new Order();
        order.setId(requirement.getOrderid());
        //order.setTotalprice(requirement.getPrice().doubleValue());
        order.setTotalprice(0.01);
        orderDao.update(order);

        return requirementDao.update(requirement)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.requirementDao.deleteById(id) > 0;
    }


    @Override
    public Rsp deleteLogical(Requirement requirement) {

        requirement.setIsdeleted(1);

        return requirementDao.update(requirement)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }
}